package com.daykw.ip.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.quartz.DisallowConcurrentExecution;
import org.springframework.stereotype.Service;

import com.daykw.ip.http.HttpBase;
import com.daykw.ip.model.Ips;
import com.daykw.ip.util.Const;
import com.daykw.ip.util.SpringContextHolder;

@DisallowConcurrentExecution
@Service
public class HCTIpProxyTask extends CustmTask {
	private static Logger logger = Logger.getLogger(HCTIpProxyTask.class);

	@Override
	public void doStart() {

		try {
			/**
			 * 5分钟切换一次ip
			 */

			if (QuartzManager.getTrigger("HCTIpProxyTask") == null) {
				QuartzManager.addJob("HCTIpProxyTask", this, "0/20 * * * * ?");
			} else {
				logger.error("############已经存在任务：HCTIpProxyTask############");
			}
		} catch (Exception e) {
			logger.error("", e);
		}

	}//

	@Override
	public void doWork() {
		try {
			if (SpringContextHolder.getApplicationContext() == null) {
				try {
					TimeUnit.SECONDS.sleep(3);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				logger.info("等待上下文启动");
			}
			//NutDao xhsDao = SpringContextHolder.getBean("xhsDao");
			NutDao bdsDao = SpringContextHolder.getBean("bdsDao");
			DateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String nowtime = FORMAT.format(new Date());

			try {
				String url = Const.IP_HCT;
				String iplist = new HttpBase().get(url, "utf-8", null);

				String[] proxys = iplist.split("\r\n");
				if (proxys.length == 1) {
					proxys = iplist.split("\r");
				} else if (proxys.length == 1) {
					proxys = iplist.split("\n");
				}

				int xcount = 0;
				int bcount = 0;
				if (proxys != null && proxys.length > 0) {
					for (int i = 0; i < proxys.length; i++) {

						String ips = proxys[i];
						String ip = ips.split(":")[0];
//						Ips isip = xhsDao.fetch(Ips.class, Cnd.where("ips", "=", ips));
//						if (isip == null) {
//							isip = new Ips();
//							isip.setIps(ips);
//							isip.setCreate_time(new Date());
//							isip.setOrderby(3);
//							try {
//								xhsDao.fastInsert(isip);
//								xcount++;
//							} catch (Exception e) {
//
//							}
//						}

						Ips bdsip = bdsDao.fetch(Ips.class, Cnd.where("ips", "=", ips));
						if (bdsip == null) {
							bdsip = new Ips();
							bdsip.setIps(ips);
							bdsip.setCreate_time(new Date());
							bdsip.setOrderby(3);
							try {
								bdsDao.fastInsert(bdsip);
								bcount++;
							} catch (Exception e) {

							}
						}

					}
					//logger.info("#############IP_HCT已经注册到缓存:" + xcount + "个IP," + nowtime + "#############");
					logger.info("#############IP_HCT已经注册到缓存:" + bcount + "个IP," + nowtime + "#############");
				}

			} catch (Exception e) {
				logger.error("", e);
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}
}
